<?php

namespace App\Http\Utils\Integral;

use Illuminate\Support\Facades\DB;

/**
 * Class Manual
 * @package App\Http\Utils\Integral
 */
class Manual
{
    use UserContainer, ShopContainer;
    /**
     * @var string
     */
    private $shopId;
    /**
     * @var array
     */
    private $userIds;
    /**
     * @var int
     */
    private $return;
    /**
     * @var int
     */
    private $every;

    /**
     * @var array
     */
    private $role;

    /**
     * Manual constructor.
     * @param string $shopId
     * @param array $userIds
     * @param int $return
     * @param int $every
     */
    public function __construct($shopId, array $userIds, $return, $every)
    {
        $this->shopId = $shopId;
        $this->userIds = $userIds;
        $this->return = $return;
        $this->every = $every;
        $this->role = [
            "day_every"  => $this->every,
            "day_return" => $this->return,
        ];
    }

    /**
     * @throws \Throwable
     */
    public function run()
    {
        DB::beginTransaction();
        try {
            $shop = $this->shop($this->shopId);
            foreach ($this->userIds as $userId) {
                $user = $this->user($userId, $this->shopId);
                if ($shop->mal_enabled && $user->mal_enabled) {
                    if ($shop->totalAmount < $this->every) {
                        $taskId = DB::table('task')->insertGetId([
                            'enabled'           => 'true',
                            'user_id'           => $userId,
                            'order_num'         => "",
                            'shop_id'           => $this->shopId,
                            'pistolgraph'       => json_encode($this->role),
                            'type'              => "manual",
                            'grant_integral'    => 0,
                            'unissued_integral' => 0,
                            'count'             => $this->return,
                            'disable'           => "true",
                            'create_time'       => Time::current(),
                            'update_time'       => Time::current(),
                        ]);
                        $user->flow("", 'fail', '商户积分池的积分已发放完毕', '手动积分红包', 0, $taskId);
                    } else {
                        $taskId = DB::table('task')->insertGetId([
                            'enabled'           => 'true',
                            'user_id'           => $userId,
                            'order_num'         => "",
                            'shop_id'           => $this->shopId,
                            'pistolgraph'       => json_encode($this->role),
                            'type'              => "manual",
                            'grant_integral'    => $this->every,
                            'unissued_integral' => 0,
                            'count'             => $this->return - $this->every,
                            'disable'           => "true",
                            'create_time'       => Time::current(),
                            'update_time'       => Time::current(),
                        ]);
                        $user->flow("", 'success', 'success', '手动积分红包', $this->every, $taskId);
                        $shop->flow("", $userId, -$this->every, '手动积分红包', $taskId);
                    }
                } else {
                    DB::table('task')->insert([
                        'enabled'           => 'true',
                        'user_id'           => $userId,
                        'order_num'         => "",
                        'shop_id'           => $this->shopId,
                        'pistolgraph'       => json_encode($this->role),
                        'type'              => "manual",
                        'grant_integral'    => 0,
                        'unissued_integral' => 0,
                        'count'             => $this->return,
                        'disable'           => "true",
                        'create_time'       => Time::current(),
                        'update_time'       => Time::current(),
                    ]);
                }
            }
            $this->shopRecord();
            $this->userRecord();
            DB::commit();
        } catch
        (\Throwable $throwable) {
            DB::rollBack();
            throw $throwable;
        }
    }
}